package com.bwie.service.impl;

import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Activity;
import com.bwie.common.domain.Lottery;
import com.bwie.common.domain.Record;
import com.bwie.common.domain.User;
import com.bwie.common.domain.response.AddResponse;
import com.bwie.common.oss.OssUtil;
import com.bwie.common.utils.JwtUtils;
import com.bwie.mapper.ActivityMapper;
import com.bwie.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @className: ActivityServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 房新路
 * @date: 2025/8/7 20:13
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OssUtil ossUtil;
    @Override
    public PageInfo<Activity> activityList(Activity activity) {
        PageHelper.startPage(activity.getPageNum(),activity.getPageSize());
        List<Activity> list=activityMapper.activityList(activity);
        PageInfo<Activity> activityPageInfo = new PageInfo<>(list);
        return activityPageInfo;
    }

    @Override
    public PageInfo<Record> recordList(Record record) {
        PageHelper.startPage(record.getPageNum(),record.getPageSize());
        List<Record> list=activityMapper.recordList(record);
        PageInfo<Record> recordPageInfo = new PageInfo<>(list);
        return recordPageInfo;
    }

    @Override
    @Transactional
    public void activityAdd(AddResponse addResponse) {
         //添加活动表
        activityMapper.activityAdd(addResponse);
        //添加奖品规则
        String activityName = addResponse.getActivityName();
        Integer actId=activityMapper.selectActivityId(activityName);
        Lottery lottery = new Lottery();
        lottery.setActivityId(actId);
        activityMapper.lotteryAdd(addResponse);
        //添加记录表
        String awardName = addResponse.getAwardName();
        Lottery lot =activityMapper.selectLotteryIdActId(awardName);
        Integer lotteryId = lot.getId();
        Integer activityId = lot.getActivityId();
        Record record = new Record();
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        User user = (User) redisTemplate.opsForValue().get(userKey);
        Integer userId = user.getId();
        record.setUserId(userId);
        record.setLotteryId(lotteryId);
        record.setActivityId(activityId);
        record.setDrawTime(new Date());
        activityMapper.recordAdd(addResponse);
    }

    @Override
    public String upload(MultipartFile[] files) {
        //创建一个String的类放多图片
        String PathFile="";
        //循环遍历
        for (MultipartFile file : files) {
            String s = ossUtil.uploadMultipartFile(file);
            PathFile+=s+";";
        }
        return PathFile;
    }

}